package org.hxzon.cyclone.plinks;

import java.util.List;

import org.hxzon.cyclone.Vector3;
import org.hxzon.cyclone.pcontacts.ParticleContact;

//电缆，绳索，拉伸时产生约束
public class ParticleCable extends ParticleLink {
    //绳索最大长度，即拉直时的长度
    public double maxLength;

    //绳索的恢复系数(bounciness，反弹力)
    public double restitution;

    public int addContact(List<ParticleContact> contacts) {
        double length = currentLength();

        //只有拉伸时产生约束
        if (length < maxLength) {
            return 0;
        }

        ParticleContact contact = new ParticleContact();
        contacts.add(contact);

        contact.particle[0] = particle[0];
        contact.particle[1] = particle[1];

        // 碰撞法向量
        Vector3 normal = particle[1].getPosition().minus(particle[0].getPosition());
        normal.normalise();
        contact.contactNormal = normal;

        contact.penetration = length - maxLength;//形变长度
        contact.restitution = restitution;

        return 1;
    }

}
